package com.example.mvc.controller;

import com.example.mvc.bean.Book;
import com.example.mvc.bean.User;
import com.example.mvc.service.BookService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/queryBookListServlet")
public class QueryBookListServlet extends HttpServlet {
    BookService bookService= new BookService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
      this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String queryBookName = req.getParameter("queryBookName");
        List<Book> bookList = bookService.getQueryBookList(queryBookName);
        HttpSession session = req.getSession();
        session.setAttribute("bookList",bookList);
        User user = (User) session.getAttribute("user");
        int status = user.getStatus();
        String contextPath = req.getContextPath();
        if (status==1){//来自普通用户界面查询，就调到普通用户
            resp.sendRedirect(contextPath+"/user_index.jsp");
        }else {
            resp.sendRedirect(contextPath+"/admin_index.jsp");
        }

    }

}
